package mapReduce.demo05_shuffle.demo04_monthTempCount;

import mapReduce.util.JobSubmit;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class MonthTempCountApp {

    public static class MTCMapper extends Mapper<LongWritable, Text,Text, IntWritable>{

        Text outKey = new Text();
        IntWritable outValue = new IntWritable();

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] fields = line.split("\t");

            outKey.set(fields[0]);
            outValue.set(Integer.parseInt(fields[1]));

            context.write(outKey,outValue);
        }
    }

    public static class MTCReducer extends Reducer<Text,IntWritable,Text,IntWritable>{

        IntWritable outValue = new IntWritable();

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            int sum = 0;

            for (IntWritable value : values) {
                sum+=value.get();
            }

            outValue.set(sum);

            context.write(key,outValue);

        }
    }

    public static void main(String[] args) {

        JobSubmit.submitBaseJob(MonthTempCountApp.class,args,MonthTempCountCombiner.class);

    }
}
